package org.tloss;

public class WhiteMonster extends AbstractMonster {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3738778761596674291L;

	public boolean moveStep(Status status) throws Exception {
		Location manLocation = status.getManLocation();
		boolean rs = false;
		int dy = manLocation.getY() - location.getY();
		if (dy > 0) {
			Location newLocation = (Location) location.clone();
			newLocation.setY(newLocation.getY() + 1);
			if (Utils.canMove(location, newLocation, status)) {
				location = newLocation;
				rs = true;
			}
		} else if (dy < 0) {
			Location newLocation = (Location) location.clone();
			newLocation.setY(newLocation.getY() - 1);
			if (Utils.canMove(location, newLocation, status)) {
				location = newLocation;
				rs = true;
			}
		} else {
			int dx = manLocation.getX() - location.getX();
			if (dx > 0) {
				Location newLocation = (Location) location.clone();
				newLocation.setY(newLocation.getX() + 1);
				if (Utils.canMove(location, newLocation, status)) {
					location = newLocation;
					rs = true;
				}
			} else if (dx < 0) {
				Location newLocation = (Location) location.clone();
				newLocation.setY(newLocation.getX() - 1);
				if (Utils.canMove(location, newLocation, status)) {
					location = newLocation;
					rs = true;
				}
			}
		}
		return rs;
	}

	public Status move(Status status) throws Exception {
		if (!catchMan(status)) {
			boolean move = moveStep(status);
			if (move) {
				System.out.println("+:"+move);
				status.itemsEffect(Status.AFFTER_MONSTER_MOVE);
				if (!catchMan(status)) {
					move = moveStep(status);
					if (move)
						status.itemsEffect(Status.AFFTER_MONSTER_MOVE);
				}
			}
		}
		return status;
	}

	public Object clone() throws CloneNotSupportedException {
		WhiteMonster monster = new WhiteMonster();
		if (location != null) {
			monster.location = (Location) location.clone();
		} else {
			monster.location = null;
		}
		return monster;
	}
	
}
